﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Jade.Tools.SL.Controls.ImageViewer
{
    public partial class ItemHolder : UserControl
    {
        public delegate void OnItemHolderClick(object sender, MouseButtonEventArgs e);
        public delegate void OnMouseWheelItemHolder(object sender, MouseWheelEventArgs e);

        #region private Member
        private String file;
        #endregion

        #region public Member

        /// <summary>
        /// Url Image
        /// </summary>
        public String File
        {
            get { return file; }
            set
            {
                try
                {
                    file = value;
                    //set if its an image
                    if (file != null)
                    {
                        if (file.IsImageFile())
                        {
                            ToolTipService.SetToolTip(this, file.GetFileName());
                            Uri uri = new Uri(file, UriKind.RelativeOrAbsolute);
                            BitmapImage b = new BitmapImage(uri);
                            imgSource.Source = b;
                        }


                    }
                }
                catch
                {
                    System.Diagnostics.Debug.WriteLine(string.Format(
                        "Problem with file {0} within ItemHolder property Setter",
                        file));
                }
            }
        }

        public event OnItemHolderClick ItemHolderClick;
        public event OnMouseWheelItemHolder ItemHolderMouseWheel;

        #endregion

        public ItemHolder(String file)
        {
            InitializeComponent();
            File = file;
            this.MouseLeftButtonDown += ItemHolder_MouseLeftButtonDown;
            this.MouseWheel += ItemHolder_MouseWheel;
        }

        #region private Method

        void ItemHolder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ItemHolderClick(sender, e);
        }

        void ItemHolder_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ItemHolderMouseWheel(sender, e);
        }

        #endregion

        #region public Method

        /// <summary>
        /// Active Border
        /// </summary>
        /// <param name="active"></param>
        public void ActiveBorder(bool active)
        {
            if (active)
            {
                BorderMain.BorderBrush = new SolidColorBrush(Colors.White);
                BorderMain.BorderThickness = new Thickness(2, 2, 2, 2);
                this.Opacity = 1;
            }
            else
            {
                BorderMain.BorderThickness = new Thickness(1, 1, 1, 1);
                BorderMain.BorderBrush = BorderBrushColor;
                this.Opacity = 0.6;
            }
        }

        /// <summary>
        /// Select Border
        /// </summary>
        /// <param name="active"></param>
        public void SelectBorder(bool active)
        {
            if (active)
            {
                BorderMain.BorderBrush = new SolidColorBrush(Colors.Red);
                BorderMain.BorderThickness = new Thickness(2, 2, 2, 2);
                this.Opacity = 1;
            }
            else
            {
                BorderMain.BorderThickness = new Thickness(1, 1, 1, 1);
                BorderMain.BorderBrush = BorderBrushColor;
                this.Opacity = 0.6;
            }
        }


        #endregion
    }
}
